/**
 * Copyright 2018-2019 jianggujin (www.jianggujin.com).
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.jianggujin.modulelink.support;

import java.util.Map;

import com.jianggujin.modulelink.exception.JBeanNotFoundException;

/**
 * Bean提供者服务抽象实现
 * 
 * @author jianggujin
 *
 */
public abstract class JAbstractBeanProvider implements JBeanProvider {

    @Override
    public <T> T getBean(Class<T> requiredType) throws JBeanNotFoundException {
        T bean = this.optBean(requiredType);
        if (bean == null) {
            throw new JBeanNotFoundException("could not found bean with requiredType:" + requiredType.getName());
        }
        return bean;
    }

    @Override
    public Object getBean(String name) throws JBeanNotFoundException {
        Object bean = this.optBean(name);
        if (bean == null) {
            throw new JBeanNotFoundException("could not found bean with name:" + name);
        }
        return bean;
    }

    @Override
    public <T> T getBean(String name, Class<T> requiredType) throws JBeanNotFoundException {
        T bean = this.optBean(name, requiredType);
        if (bean == null) {
            throw new JBeanNotFoundException(
                    "could not found bean with name:" + name + ", and requiredType:" + requiredType.getName());
        }
        return bean;
    }

    @Override
    public String[] getBeanNamesForType(Class<?> requiredType) throws JBeanNotFoundException {
        String[] names = this.optBeanNamesForType(requiredType);
        if (names == null) {
            throw new JBeanNotFoundException("could not found bean with requiredType:" + requiredType.getName());
        }
        return names;
    }

    @Override
    public <T> Map<String, T> getBeansOfType(Class<T> requiredType) throws JBeanNotFoundException {
        Map<String, T> beans = this.optBeansOfType(requiredType);
        if (beans == null) {
            throw new JBeanNotFoundException("could not found bean with requiredType:" + requiredType.getName());
        }
        return beans;
    }
}
